package com.stu.day18.ReflexPractice;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author zy
 * @date 2025/2/20 15:44
 */
public class ORMDemo {
    public static void main(String[] args) throws Exception {
        List<String> list = new ArrayList<>();  // 存储所有符合要求的.java文件
        listAll("src/com/stu/day18/ReflexPractice/entity",list);

        for(String s : list){
            System.out.println(s);
            createTable(s);
            System.out.println("-------------------------");
        }
    }

    public static void createTable(String tableName) throws ClassNotFoundException {
        List<String> list = new ArrayList();

        Class<?> clazz = Class.forName(tableName);
        String name = "";
        if(clazz.isAnnotationPresent(Table.class)){  // 通过反射获取注解对象
            Table myTable = clazz.getDeclaredAnnotation(Table.class);
            name = myTable.name();  // 获取注解的属性信息
        }
        String str = "create table "+name+"(\n";
        list.add(str);

        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String s = "  ";
            //属性名称
            if(field.isAnnotationPresent(Id.class)){
                s += field.getName();
            }
            else if(field.isAnnotationPresent(Column.class)){
//                s = clazz.getDeclaredAnnotation(Column.class).name();
                Column column = field.getAnnotation(Column.class); // 使用 field.getAnnotation() 获取字段注解
                s += column.name();  // 获取字段级注解的属性
            }
            else{
                s += convertToSnakeCase(field.getName());
            }

            //属性类型
            Class<?> type = field.getType();
            String fieldName = switch (type.getSimpleName()) {
//                case "int" -> " int";
                case "Integer" -> " int";
                case "Long" -> " bigint";
                case "long" -> " bigint";
                case "String" -> " varchar(20)";
                case "char" -> " varchar(20)";
//                case "Date" -> " date";
//                case "Double" -> " double";
//                case "double" -> " double";
                default -> " "+type.getSimpleName().toLowerCase();
            };
            s += fieldName;

            //key等情况
            if(field.isAnnotationPresent(Id.class)){
                s += " primary key";
            }
            else if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getAnnotation(Column.class);  // 获取字段级的注解
                if (column.nullable()) {
                    s += " null";
                } else {
                    s += " not null";
                }
            }
            s += ",\n";
            list.add(s);
        }

        list.add(");\n");

        for (String s : list) {
            System.out.print(s);
        }
    }

    public static void listAll(String path,List<String> list){
        File file = new File(path);
        if(!file.exists()) {
            System.out.println(file+" 不存在");
            return;
        }

        File[] files = file.listFiles();
        if(files==null){
            System.out.println("无法读取文件夹: " + file.getPath());
            return;
        }
        for (File f : files) {
            String str = convertToClassName(f.toString());
            list.add(str);
        }
    }

    public static String convertToSnakeCase(String input) {
        // StringBuilder 用于构建最终的字符串
        StringBuilder result = new StringBuilder();

        // 遍历每个字符
        for (char c : input.toCharArray()) {
            // 如果是大写字母
            if (Character.isUpperCase(c)) {
                // 如果 result 不是空，则加上下划线
                if (result.length() > 0) {
                    result.append('_');
                }
                // 将大写字母转换为小写字母并追加到结果中
                result.append(Character.toLowerCase(c));
            } else {
                // 如果是小写字母，直接追加
                result.append(c);
            }
        }
        return result.toString();
    }

    public static String convertToClassName(String filePath) {
        // 去掉路径中的 src\ 部分
        String classPath = filePath.replace("src\\", "");

        // 替换路径中的 \ 为 .
        classPath = classPath.replace("\\", ".");

        // 去掉 .java 后缀
        if (classPath.endsWith(".java")) {
            classPath = classPath.substring(0, classPath.length() - 5);  // 去除 ".java" 的长度
        }
        return classPath;
    }
}